package com.cys.dm.rest.service;

import com.cys.dm.client.base.DmImageClient;
import com.cys.dm.client.item.DmItemClient;
import com.cys.dm.client.item.DmItemTypeClient;
import com.cys.dm.entity.base.DmImage;
import com.cys.dm.entity.item.DmItem;
import com.cys.dm.entity.item.DmItemType;
import com.cys.dm.vo.CommonResponse;
import com.cys.dm.vo.VoUtil;
import com.cys.dm.vo.dmitem.DmItem2Response;
import com.cys.dm.vo.dmitem.DmItemResponse;
import com.cys.dm.vo.dmitem.DmItemTypeResponse;
import com.cys.dm.vo.dmitem.FloorResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class DmItemTypeService {

    @Resource
    private DmItemClient dmItemClient;
    @Resource
    private DmItemTypeClient dmItemTypeClient;
    @Resource
    private DmImageClient dmImageClient;

    /**
     * 纵向菜单及相关热门
     * @return
     */
    public CommonResponse<List<DmItemTypeResponse>> nav(){
        List<DmItemType> parentTypes = dmItemClient.dmItemTypeList(0L);
        List<DmItemTypeResponse> dmItemTypeResponses = new ArrayList<>();
        parentTypes.forEach(parentType->{
            DmItemTypeResponse dmItemTypeResponse = new DmItemTypeResponse();
            BeanUtils.copyProperties(parentType,dmItemTypeResponse);

            List<DmItemType> childrenTypes = dmItemClient.dmItemTypeList(parentType.getId());
            List<DmItemTypeResponse> dmItemTypeResponses1 = new ArrayList<>();
            childrenTypes.forEach(childType->{
                DmItemTypeResponse dmItemTypeResponse1 = new DmItemTypeResponse();
                BeanUtils.copyProperties(childType,dmItemTypeResponse1);
                dmItemTypeResponses1.add(dmItemTypeResponse1);
            });
            dmItemTypeResponse.setChildren(dmItemTypeResponses1);

            //商品查询
            List<DmItem> dmItems = dmItemClient.hotItemList(parentType.getId(),1L,1L);
            List<DmItem2Response> dmItem2Responses = new ArrayList<>();
            dmItems.forEach(dmItem -> {
                DmItem2Response dmItem2Response = new DmItem2Response();
                BeanUtils.copyProperties(dmItem,dmItem2Response);
                DmImage dmImage = dmImageClient.findItenPicByID(dmItem.getId());
                dmItem2Response.setImgUrl(dmImage.getImgUrl());
                dmItem2Responses.add(dmItem2Response);
            });
            dmItemTypeResponse.setHotItems(dmItem2Responses);

            dmItemTypeResponses.add(dmItemTypeResponse);
        });

        return VoUtil.returnSuccess("查询商品分类成功",dmItemTypeResponses);
    }

    public CommonResponse<List<DmItemType>> lineNav(){
        List<DmItemType> dmItemTypes = dmItemClient.findBylinAsNav();
        return VoUtil.returnSuccess("查询横向导航栏成功",dmItemTypes);
    }

    /**
     * 楼层及商品信息
     * @return
     */
    public CommonResponse<List<FloorResponse>> floor(){
        List<FloorResponse> floorResponses = new ArrayList<>();
        List<DmItemType> dmItemTypes = dmItemTypeClient.postFloorData();
        dmItemTypes.forEach(dmItemType -> {
            FloorResponse floorResponse = new FloorResponse();
            floorResponse.setIndex(dmItemType.getId());
            floorResponse.setItemTypeName(dmItemType.getItemType());
            floorResponse.setItemTypeId(dmItemType.getId());
            //添加商品
            List<DmItem> dmItems = dmItemClient.queryFloorItems(dmItemType.getId());
            floorResponse.setItems(new ArrayList<>());
            dmItems.forEach(dmitem->{
                //数据复制到Vo
                DmItemResponse dmItemResponse = new DmItemResponse();
                dmItemResponse.setId(dmitem.getId());
                dmItemResponse.setItemName(dmitem.getItemName());
                dmItemResponse.setStartDate(dmitem.getStartTime());
                dmItemResponse.setEndDate(dmitem.getEndTime());
                //根据ID查询图片
                DmImage dmImage = dmImageClient.findItenPicByID(dmitem.getId());
                dmItemResponse.setImgUrl(dmImage.getImgUrl());
                dmItemResponse.setMinPrice(dmitem.getMinPrice());
                floorResponse.getItems().add(dmItemResponse);
            });
            floorResponses.add(floorResponse);
        });
        return VoUtil.returnSuccess("楼层查询成功",floorResponses);
    }

}
